<template>
  <div class="login-page">
    <el-card>
      <template #header>
        面试经验运营管理后台
      </template>
      <div>
        <el-form :model='form' :rules='rules' ref='myForm' @keyup.enter.native="clickLogin">
          <el-form-item label='用户名：' prop='username'>
            <el-input placeholder='请输入用户名' v-model="form.username"></el-input>
          </el-form-item>

          <el-form-item label='密码：' prop='password' >
            <el-input type='password' placeholder='请输入密码' v-model="form.password"></el-input>
          </el-form-item>

          <el-form-item class='tc'>
            <el-button type='primary' @click="clickLogin" >登 录</el-button>
            <el-button @click="reset">重 置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/login'
import { setToken } from '@/utils/storage'
export default {
  name: 'LoginPage',

  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username:
        [
          { required: true, message: '用户名不能为空', trigger: ['blur', 'change'] },
          { min: 4, max: 14, message: '请输入4-14位长度的用户名', trigger: ['blur', 'change'] }
        ],
        password:
        [
          { required: true, message: '密码不能为空', trigger: ['blur', 'change'] },
          { min: 5, max: 20, message: '请输入5-20位长度的密码', trigger: ['blur', 'change'] }
        ]

      }
    }
  },

  methods: {
    async clickLogin () {
      try {
        await this.$refs.myForm.validate()

        const res = await login(this.form)
        console.log(res.data.token)
        this.$store.commit('user/setStoreToken', res.data.token)
        setToken(res.data.token)
        this.$router.push('/')
      } catch (error) {
        console.log(error)
      }
    },
    reset () {
      this.$refs.myForm.resetFields()
    }
  }
}
</script>

<style lang='scss' scoped>
  .login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header{
      height: 80px;
      background: rgba(114,124,245,1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}

</style>
